package actions;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.AdminControlPanel;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;

import beans.BackupRestoreForm;
import beans.ThemeForm;

import dto.Themes;

public class ThemeAction extends DispatchAction{

	public ThemeAction() {
		// TODO Auto-generated constructor stub
	}
	 public ActionForward getAllThemes(ActionMapping mapping, ActionForm form, HttpServletRequest request,
             HttpServletResponse response)
throws Exception {
		 ActionForward forward = new ActionForward();
		 AdminControlPanel acp = new AdminControlPanel();
		 List<Themes> themes=acp.getThemes();
		 request.setAttribute("themes", themes);
		 forward=mapping.findForward("themes");
		 return forward;
	 }
	 
	 public ActionForward addTheme(ActionMapping mapping, ActionForm form, HttpServletRequest request,
             HttpServletResponse response)
throws Exception {
		 ActionForward forward = new ActionForward();
		 AdminControlPanel acp = new AdminControlPanel();
		 
		 
		    FormFile file=((ThemeForm)form).getFile();	
		    String name=((ThemeForm)form).getThemename();
                    if(!(name==null || name.trim().equals("") || file==null)){
                         acp.addTheme(name,file);
                         
                    }else{
                        if(name==null || name.trim().equals("")){
                            System.out.println("name error...................");
                            ActionErrors errors = new ActionErrors();
                        errors.add("NameError", new ActionMessage("ThemeNameError"));
                        saveErrors(request, errors);
                        }
                        if(file==null || file.getFileName()==null || file.getFileData().length==0 || file.getFileName().trim().equals("")){
                            System.out.println("file error...................");
                            ActionErrors errors = new ActionErrors();
                        errors.add("FileError", new ActionMessage("ThemeFileError"));
                        saveErrors(request, errors);
                        }
                         
                    }
                    List<Themes> themes=acp.getThemes();
                         request.setAttribute("themes", themes);
                    ((ThemeForm)form).setThemename("");
		 forward=mapping.findForward("themes");
		 return forward;
	 }
	 
	 public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
             HttpServletResponse response)
throws Exception {
		 ActionForward forward = new ActionForward();
		 AdminControlPanel acp = new AdminControlPanel();
		 String themename=((ThemeForm)form).getThemename();
		 boolean check=acp.deleteTheme(themename);
		 if(check){
			 List<Themes> themes=acp.getThemes();
			 request.setAttribute("themes", themes); 
		 }else{
			 ActionErrors errors = new ActionErrors();

	            errors.add("DeleteThemeError", new ActionMessage("DeleteThemeError"));
	            saveErrors(request, errors);
		 }
		 ((ThemeForm)form).setThemename("");
		 forward=mapping.findForward("themes");
		 return forward;
	 }
	 
	 public ActionForward preview(ActionMapping mapping, ActionForm form, HttpServletRequest request,
             HttpServletResponse response)
throws Exception {
		 ActionForward forward = new ActionForward();
		 AdminControlPanel acp = new AdminControlPanel();
		 String themename=((ThemeForm)form).getThemename();
		 Themes theme=acp.getTheme(themename);
		 request.setAttribute("theme",theme);		 
		 forward=mapping.findForward("preview");
		 return forward;
	 }

}
